<?php
if(!defined('BASEPATH'))
        exit('No direct script access allowed');
    /**
     * Main class for working with galery and prictures
     */
    class gallery extends CI_Controller {

        private $data;

        function __construct() {

            parent::__construct();

            if (!$this->session->userdata('logged_in'))
                redirect(base_url('login'));

            $this->load->config('languages');
            $this->load->config('gallery');// ovdje je page_types

            $this->load->model('posts_model');
            $this->load->model('gallery_model');

            $this->translate = & $this->factory->create('translate', 'foo', 'bar');
            $this->activeLang = $this->session->userdata('my_language');
        }

        public function index() {
            if (!isset($_GET['post_type']))
                $_GET['post_type'] = "page";

            $this->view_all();
        }

        /**
         * @param $_GET['post_type']
         */
        function view_all() {

            $this->data['page'] = 'Gallery';
            $this->data['title'] = 'View all Galleries';
            $this->data['post_type'] = $_GET['post_type'];

            $this->data['all_galleries'] = $this->gallery_model->view_all($_GET['type']);

//            $this->firephp->log($this->data['all_galleries']);

            if ($_GET['type'] == 'all')
                $this->template->load('gallery/view_all_all', $this->data);
            else
                $this->template->load('gallery/view_all', $this->data);
        }

        function view_new() {
            $this->data['page'] = 'Gallery';
            $this->data['title'] = 'Add Gallery';
            $this->data['all_galleries'] = $this->gallery_model->view_all(NULL);
            $this->template->load('gallery/add_gallery_images', $this->data);
        }

        /**
         * 
         * @param $_GET['post_ID']
         * @param $_GET['post_type']
         */
        function view_edit() {
            $this->data['post_action'] = "EDIT";
            $this->data['page'] = 'Gallery';
            $this->data['title'] = 'Edit Gallery';
            $this->data['post_type'] = $_GET['post_type'];
            $this->data['gallery'] = $this->gallery_model->view_edit($_GET['gallery_ID']);
            $this->data['images'] = $this->gallery_model->view_edit_images($_GET['gallery_ID']);
            $this->template->load('gallery/edit_gallery', $this->data);
        }

        function submit_gallery() {

            $message = "Something went wrong. Please try again.";

            switch ($_POST['post_action']) {

                case 'new-gallery':

                    $result = $this->gallery_model->add_gallery(
                            $_POST['type'], $_POST['galleryname'], $_POST['parent_id']
                    );
                    if ($result)
                        $message = 'Successful add gallery operation.';

                    break;

                case 'edit-gallery':

                    $result = $this->gallery_model->edit_gallery(
                            $_POST['gallery_ID'], $_POST['galleryname'], $_POST['linkto'], $_POST['parent_id']
                    );

                    if ($result)
                        $message = 'Successful edit gallery operation.';

                    break;

                case 'new-pictures':

                    //load reasize library
                    $this->load->library('img_map');

                    $result = $this->gallery_model->add_pictures(
                            $_POST['galleryselect'], $_POST['pictures'], $_POST['pic_titles']
                    );

                    if ($result)
                        redirect(base_url('gallery/view_all?post_type=gallery&type=all'));
                    break;
//
//                case 'new-videos': $this->gallery_model->create_videos();
//                    break;
            }

            echo json_encode(array('action' => true, 'msg' => $message));
        }

        function delete($ID) {
            $result = $this->gallery_model->delete($ID);

            if ($result)
                redirect(base_url('gallery/view_all?post_type=gallery&type=all'));
            else
                show_error('Error ocured while deleting gallery!');
        }

        function delete_picture() {
            $result = $this->gallery_model->delete_picture($_GET['picture_ID'], $_GET['gallery_ID']);

            if ($result)
                redirect(base_url('gallery/view_edit?gallery_ID=' . $_GET['gallery_ID'] . '&post_type=gallery'));
            else
                show_error('Something went wrong. Delete picture fails.');
        }

        function picture_order() {
            $result = $this->gallery_model->picture_order($_POST['gallery_ID'], $_POST['picture-order']);

            if ($result)
                redirect(base_url('gallery/view_edit?gallery_ID=' . $_POST['gallery_ID'] . '&post_type=gallery'));
            else
                show_error('Something went wrong. Order pictures fails.');
        }

    }

?>
